package com.cci.kangdao.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cci.kangdao.externalData.service.ExternalDataSynchroService;
import com.cci.kangdao.service.SyncEsnAndUserId;
import com.cci.kangdao.utilTool.CollectionUtils;
import com.cci.kangdao.utilTool.ParameterTool;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description:
 * @author: joe
 * @company: CTY
 * @since: 2023/2/6 11:39
 * @version: V1.0
 **/
@Controller
@RequestMapping("syncEsnAndUser")
public class SyncEsnAndUserIdController {
    @Resource
    private SyncEsnAndUserId syncEsnAndUserId;
    @Resource
    private ExternalDataSynchroService externalDataSynchroService;

    private Logger log = Logger.getLogger(this.getClass().getName());

    @RequestMapping("syncByUserId.aspx")
    public void syncEsnAndUser(@RequestParam("userId") Long userId, @RequestParam("type") Byte type, HttpServletResponse response) {
        if (null == userId || null == type) {
            String msg = "{\"status\": -99,\"msg\": \"缺少  userId或type 参数\"}";
            ParameterTool.writeResponse(response, msg);
            return;
        }
        if (type == 1) {
            //syncEsnAndUserId.asSyncEsnAndUserIdAddByuserId(userId);
        } else if (type == 2) {
            externalDataSynchroService.deleteEsnAndFleet(userId);
        } else {
            String msg = "{\"status\": -99,\"msg\": \"type 参数不正确\"}";
            ParameterTool.writeResponse(response, msg);
            return;
        }
        String msg = "{\"status\": 0,\"msg\": \"调用成功\"}";
        ParameterTool.writeResponse(response, msg);
        return;
    }

    @RequestMapping("syncByUserIdList.aspx")
    public void syncByUserIdList(@RequestBody String paramStr, HttpServletResponse response) {
        JSONObject jsonObject = JSON.parseObject(paramStr);
        Integer type = jsonObject.getInteger("type");
        if (null ==  jsonObject.get("list") || null == type) {
            String msg = "{\"status\": -99,\"msg\": \"缺少  list或type 参数\"}";
            ParameterTool.writeResponse(response, msg);
            return;
        }
        log.error("传入的参数"+ paramStr);
        List<Long> list = JSON.parseArray(JSON.toJSONString(jsonObject.get("list")), Long.class);
        if (CollectionUtils.isNotEmpty(list)) {
            for (Long userId : list) {
                if (type == 1) {
                    syncEsnAndUserId.asSyncEsnAndUserIdAddByuserId(userId);
                } else if (type == 2) {
                    syncEsnAndUserId.asSyncEsnAndUserIdDelByuserId(userId);
                }
            }
        }
        String msg = "{\"status\": 0,\"msg\": \"调用成功\"}";
        ParameterTool.writeResponse(response, msg);
        return;
    }
}
